<template>
  <div style="height: 6vh; background-color:#fff;padding: 0 20px 10px;">
    <span style="line-height:60px;font-size:20px;">字典管理</span>
  </div>
  <!-- 字典数据搜索卡片 -->
  <el-card style="margin:10px 0;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="字典名称" style="width:270px;">
        <el-input placeholder="请输入" v-model="searchForm.name"
                  @keydown.enter="selectDict"></el-input>
      </el-form-item>
      <el-form-item label="字典编码" style="width:270px;">
        <el-input placeholder="请输入" v-model="searchForm.code"
                  @keydown.enter="selectDict"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="selectDict">搜索</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <el-card style="margin:10px 0;">
    <div style="margin:10px 0px 40px 10px;">
      字典列表
      <el-button type="primary" style="float: right;margin-top:-3px;" @click="addDict">新增字典</el-button>
    </div>
    <el-table :data="dictArr">
      <el-table-column type="index" label="编号" width="80" align="center"/>
      <el-table-column prop="name" label="字典名称" align="center"/>
      <el-table-column prop="code" label="字典编码" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="showDictOption(scope.row.id)">{{ scope.row.code }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注信息" align="center"/>
      <el-table-column prop="createTime" label="创建时间" align="center"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" @click="editDict(scope.row.id)">编辑</el-button>
          <el-button link type="primary" @click="deleteDict(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

  <!-- 保存字典弹窗 -->
  <el-dialog :title="dialogTitle" v-model="dialogVisible" style="width:1000px;padding:40px;"
             :before-close="handleClose">
    <el-form label-width="80px" label-position="top">
      <el-form-item label="字典名称">
        <el-input placeholder="请输入" v-model="saveDictForm.name"></el-input>
      </el-form-item>
      <el-form-item label="字典编码">
        <el-input placeholder="请输入" v-model="saveDictForm.code"></el-input>
      </el-form-item>
      <el-form-item label="备注信息">
        <el-input placeholder="请输入" v-model="saveDictForm.remark"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button type="primary" @click="saveDict">保存</el-button>
      <el-button @click="handleClose">取消</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import {onMounted, ref} from "vue";
import qs from "qs";
import axios from "axios";
import {ElMessage, ElMessageBox} from "element-plus";
import router from "@/router";
const searchForm = ref({
  name: '',
  code: ''
});
const dictArr = ref([]);
const dialogTitle = ref("");
const dialogVisible = ref(false);
const saveDictForm = ref({
  name: '',
  code: '',
  remark: ''
});
const selectDict = () => {
  let data = qs.stringify(searchForm.value);
  axios.get(BASE_URL + '/v1/dict/search?' + data).then(res=>{
    if(res.data.code == 2000) {
      dictArr.value = res.data.data;
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
onMounted(() => {
  selectDict();
});
const resetSearch = () => {
  searchForm.value = {};
  selectDict();
};
const addDict = () => {
  dialogTitle.value = '添加字典';
  dialogVisible.value = true;
};
const showDictOption = (id) => {
  router.push('/dictOption?id=' + id);
};
const handleClose = () => {
  ElMessageBox.confirm( '确定取消保存字典吗',
     '提示',
    { 
       confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    dialogVisible.value = false;
    saveDictForm.value = {};
  }).catch(() => {})
};
const saveDict = () => {
  let data = qs.stringify(saveDictForm.value);
  axios.post(BASE_URL + '/v1/dict/save', data).then(res=>{
    if(res.data.code == 2000) {
      ElMessage.success('保存成功');
      dialogVisible.value = false;
      saveDictForm.value = {};
      selectDict();
    }  else {
      ElMessage.error(res.data.msg);
    }
  });
};
const editDict = (id) => {
  dialogTitle.value = '编辑字典';
  dialogVisible.value = true;
  axios.get(BASE_URL + '/v1/dict/search?id=' + id).then (res=>{ 
    if(res.data.code == 2000) {
    saveDictForm.value = res.data.data[0];
  } else {
    ElMessage.error(res.data.msg);
  }
  });
};
const deleteDict = (id) => {
  ElMessageBox.confirm('此操作将永久删除该字典, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    axios.post(BASE_URL + '/v1/dict/delete/' + id).then(response => {
      if (response.data.code == 2000) {
        ElMessage.success('删除成功！');
        selectDict();
      } else {
        ElMessage.error(response.data.msg);
      }
    })
  }).catch(() => {})
};
</script>

<style scoped>

</style>